use serde::ser::Impossible;

#[derive(Debug)]
struct Person {
    name: String,
    age: u8,
}

// 1.定义接口 (只定义，不做实现)
trait Say {
    fn say(&self);
}

// === 封装 ===
pub fn demo_obj_pack() {
    // 2.实现
    impl Say for Person {
        fn say(&self) {
            println!("封装 -> my name is {}, and age {}.", self.name, self.age);
        }
    }

    // 3.实例
    let p1 = Person {
        name: "zds".to_string(),
        age: 18,
    };

    // 4.调用
    p1.say();
}

// === 继承 ===
pub fn demo_obj_extend() {
    // 1. 继承(struct嵌套)
    struct Engineer {
        person: Person,
        email: String,
        work: String,
    }

    // 2.实现接口
    impl Engineer {
        fn say(&self) {
            println!(
                "继承 -> my name {}, age {}, i am  {} engineer",
                self.person.name, self.person.age, self.work
            )
        }
    }

    // 3.实例
    let p1 = Engineer {
        person: Person {
            name: "zds".to_string(),
            age: 29,
        },
        email: "123@163.com".to_string(),
        work: "web".to_string(),
    };

    // 4.调用接口
    p1.say();
}

// === 多态 ===
pub fn demo_obj_all_state() {
    struct Monkey {
        name: String,
    }

    impl Say for Monkey {
        fn say(&self) {
            println!("多态 => monkey: {}", self.name)
        }
    }
    let m1 = Monkey {
        name: "zz".to_string(),
    };
    m1.say();

    struct Fish {
        name: String,
    }

    impl Say for Fish {
        fn say(&self) {
            println!("多态 => fish: {}", self.name)
        }
    }

    let f1 = Fish {
        name: "ly".to_string(),
    };
    f1.say();
}
